package com.gscitysfy.cus.drain.modules.inspection.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Maps;
import com.gscitysfy.cloudfrmwk.assistant.domain.Result;
import com.gscitysfy.cloudfrmwk.portal.feign.DictionaryService;
import com.gscitysfy.cloudfrmwk.security.definition.core.HerodotusUserDetails;
import com.gscitysfy.cloudfrmwk.security.utils.SecurityUtils;
import com.gscitysfy.cus.drain.constants.DicConstant;
import com.gscitysfy.cus.drain.modules.inspection.entity.*;
import com.gscitysfy.cus.drain.modules.inspection.mapper.*;
import com.gscitysfy.cus.drain.modules.inspection.service.IDrainInspectionMnJobService;
import com.gscitysfy.cus.drain.modules.inspection.vo.DrainInspectionMnJobExcel;
import com.gscitysfy.cus.drain.modules.inspection.vo.DrainInspectionMnJobVo;
import com.gscitysfy.cus.drain.modules.inspection.vo.MiniProgramCountVo;
import com.gscitysfy.cus.drain.utils.CoordinateTransformUtils;
import com.gscitysfy.cus.drain.utils.DateUtil;
import com.gscitysfy.cus.drain.utils.HttpResponseUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Administrator
 * @since 2022-10-14 15:00:46
 */
@Service
public class DrainInspectionMnJobServiceImpl extends ServiceImpl<DrainInspectionMnJobMapper, DrainInspectionMnJob> implements IDrainInspectionMnJobService {
    @Autowired
    DrainInspectionMnPlanMapper drainInspectionMnPlanMapper;
    @Autowired
    DrainInspectionRlUserMapper drainInspectionRlUserMapper;
    @Autowired
    DrainInspectionMnJobMapper drainInspectionMnJobMapper;

    @Autowired
    DrainInspectionRlTrackMapper drainInspectionRlTrackMapper;

    @Autowired
    DrainInspectionMnProblemMapper drainInspectionMnProblemMapper;
    @Autowired
    DrainInspectionRlPointMapper drainInspectionRlPointMapper;


    @Resource
    private DictionaryService dictionaryService;

    @Resource
    private DrainInspectionRlUserMapper userMapper;

    @Override
    public Result<Object> getByPage(Page<DrainInspectionMnJob> page, DrainInspectionMnJobVo plan) {
        LambdaQueryWrapper<DrainInspectionMnJob> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DrainInspectionMnJob::getCmUsable, false);

        if (StringUtils.isNotBlank(plan.getCmPlanBasis())) {
            queryWrapper.eq(DrainInspectionMnJob::getCmPlanBasis, plan.getCmPlanBasis());
        }
        if (StringUtils.isNotBlank(plan.getCmPlanType())) {
            queryWrapper.eq(DrainInspectionMnJob::getCmPlanType, plan.getCmPlanType());
        }
        //计划频次
        if (StringUtils.isNotBlank(plan.getCmPlanFrequency())) {
            queryWrapper.eq(DrainInspectionMnJob::getCmPlanFrequency, plan.getCmPlanFrequency());
        }
        //计划对象类别
        if (Objects.nonNull(plan.getCmPlanObjectType())) {
            queryWrapper.eq(DrainInspectionMnJob::getCmPlanObjectType, plan.getCmPlanObjectType());
        }
        if (Objects.nonNull(plan.getCmPlanCategory())) {
            queryWrapper.eq(DrainInspectionMnJob::getCmPlanCategory, plan.getCmPlanCategory());
        }
        //计划负责人
        if (StringUtils.isNotBlank(plan.getCmPlanHeaderName())) {
            queryWrapper.like(DrainInspectionMnJob::getCmPlanHeaderName, plan.getCmPlanHeaderName());
        }
        //计划执行人
        if (StringUtils.isNotBlank(plan.getCmPlanExecutorName())) {
            queryWrapper.like(DrainInspectionMnJob::getCmPlanExecutorName, plan.getCmPlanExecutorName());
        }
        if (Objects.nonNull(plan.getCmJobStatus())) {
            queryWrapper.eq(DrainInspectionMnJob::getCmJobStatus, plan.getCmJobStatus());
        }
        if (Objects.nonNull(plan.getCmJobCondition())) {
            queryWrapper.eq(DrainInspectionMnJob::getCmJobCondition, plan.getCmJobCondition());
        }
        //计划编号
        if (StringUtils.isNotBlank(plan.getCmPlanNo())) {
            queryWrapper.like(DrainInspectionMnJob::getCmPlanNo, plan.getCmPlanNo());
        }
        //计划名称
        if (StringUtils.isNotBlank(plan.getCmPlanName())) {
            queryWrapper.like(DrainInspectionMnJob::getCmPlanName, plan.getCmPlanName());
        }
        //计划开始时间范围查询
        if (Objects.nonNull(plan.getCmPlanStartDate())) {
            queryWrapper.ge(DrainInspectionMnJob::getCmPlanStartDate, plan.getCmPlanStartDate());
        }
        if (Objects.nonNull(plan.getCmPlanEndDate())) {
            queryWrapper.le(DrainInspectionMnJob::getCmPlanEndDate, plan.getCmPlanEndDate());
        }
        if (StringUtils.isNotBlank(plan.getCmJobNo())) {
            queryWrapper.like(DrainInspectionMnJob::getCmJobNo, plan.getCmJobNo());
        }
        if (plan.isPage()) {
            queryWrapper.orderByDesc(DrainInspectionMnJob::getCmAddtime);
            Page<DrainInspectionMnJob> planList = this.page(page, queryWrapper);
            return Result.content(planList);
        } else {
            //兼容数据导出,以勾选为准
            if (StringUtils.isNotBlank(plan.getIds())) {
                queryWrapper.clear();
                queryWrapper.in(DrainInspectionMnJob::getCmKid, Arrays.stream(plan.getIds().split(StrUtil.COMMA)).map(Long::parseLong).collect(Collectors.toList()));
            }
            List<DrainInspectionMnJob> planList = this.list(queryWrapper);
            return Result.content(planList);
        }
    }


    private void getStutasByCondition(String id) {
        DrainInspectionMnJob e = drainInspectionMnJobMapper.selectById(id);
        SimpleDateFormat f = new SimpleDateFormat("yyyyMMdd");
        Date time = e.getCmJobStartDate();
        //计划结束时间
        Date time1 = e.getCmJobEndDate();

        //实际开始时间
        Date time2 = e.getCmJobActualStartDate();

        //实际结束时间
        Date time3 = e.getCmJobActualEndDate();

        LambdaUpdateWrapper<DrainInspectionMnJob> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(DrainInspectionMnJob::getCmKid, e.getCmKid());
        if (Objects.nonNull(e.getCmJobStatus())) {
            if (e.getCmJobStatus() == 2) {
                if (Integer.parseInt(f.format(time1).toString()) < Integer.parseInt(f.format(time2).toString())) {
                    e.setCmJobCondition(2L);
                } else {
                    e.setCmJobCondition(1L);
                }
                drainInspectionMnJobMapper.updateById(e);
            }
            if (e.getCmJobStatus() == 3) {
                if (time3.getTime() > time1.getTime()) {
                    e.setCmJobCondition(3L);
                    updateWrapper.set(DrainInspectionMnJob::getCmJobCondition, 3L);
                } else {
                    e.setCmJobCondition(1L);
                    updateWrapper.set(DrainInspectionMnJob::getCmJobCondition, 1L);
                }
                this.update(updateWrapper);
            }
        }

    }

    @Override
    public void getStutas() {
        LambdaQueryWrapper<DrainInspectionMnJob> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DrainInspectionMnJob::getCmUsable, false);
        queryWrapper.eq(DrainInspectionMnJob::getCmJobCondition, 0L);
        queryWrapper.eq(DrainInspectionMnJob::getCmJobStatus, 2);
        SimpleDateFormat f = new SimpleDateFormat("yyyyMMdd");
        List<DrainInspectionMnJob> list = this.list(queryWrapper);

        for (DrainInspectionMnJob e : list) {
            //计划开始时间
            Date time = e.getCmJobStartDate();
            //计划结束时间
            Date time1 = e.getCmJobEndDate();
            //实际开始时间
            Date time2 = e.getCmJobActualStartDate();
            //实际结束时间
            Date time3 = e.getCmJobActualEndDate();
            LambdaUpdateWrapper<DrainInspectionMnJob> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(DrainInspectionMnJob::getCmKid, e.getCmKid());
            if (Objects.nonNull(e.getCmJobStatus())) {
                if (Integer.parseInt(f.format(time1).toString()) < Integer.parseInt(f.format(time2).toString())) {
                    e.setCmJobCondition(2L);
                } else {
                    e.setCmJobCondition(1L);
                }
                drainInspectionMnJobMapper.updateById(e);
            }
        }
        ;
    }

    @Override
    public Map<Long, List<DrainInspectionRlTrack>> trackList(String id) {
        LambdaQueryWrapper<DrainInspectionRlTrack> s = new LambdaQueryWrapper<DrainInspectionRlTrack>()
                .eq(DrainInspectionRlTrack::getCmJobId, id);
        List<DrainInspectionRlTrack> drainInspectionRlTracks = drainInspectionRlTrackMapper.selectList(s);
        Map<Long, List<DrainInspectionRlTrack>> collect = drainInspectionRlTracks.stream().collect(Collectors.groupingBy(DrainInspectionRlTrack::getCmUserId));
        return collect;
    }

    @Override
    public List<DrainInspectionMnProblem> problemList(String id) {
        LambdaQueryWrapper<DrainInspectionMnProblem> s = new LambdaQueryWrapper<DrainInspectionMnProblem>()
                .eq(DrainInspectionMnProblem::getCmJobId, id);
        return drainInspectionMnProblemMapper.selectList(s);
    }

    @Override
    public boolean del(String ids) {
        List<String> id = Arrays.asList(ids.split(","));
        LambdaUpdateWrapper<DrainInspectionMnJob> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(DrainInspectionMnJob::getCmUsable, true).in(DrainInspectionMnJob::getCmKid, id);
        return this.update(updateWrapper);
    }

    @Override
    public void export(HttpServletResponse response, DrainInspectionMnJobVo plan) throws IOException {
        plan.setPage(false);
        List<DrainInspectionMnJob> planList = (List<DrainInspectionMnJob>) this.getByPage(null, plan).getData();
        Map<String, String> jhyjMap = dictionaryService.getDictionaryMap(DicConstant.PLAN_BASIS).getData();
        Map<String, String> jhlxMap = dictionaryService.getDictionaryMap(DicConstant.PLAN_TYPE).getData();
        Map<String, String> jhdxlxMap = dictionaryService.getDictionaryMap(DicConstant.PLAN_OBJECT_TYPE).getData();
        Map<String, String> jhpcMap = dictionaryService.getDictionaryMap(DicConstant.PLAN_FREQUENCY).getData();
        planList.forEach(e -> {
            e.setCmPlanBasis(jhyjMap.get(e.getCmPlanBasis()));
            e.setCmPlanType(jhlxMap.get(e.getCmPlanType()));
            e.setCmPlanObjectType(jhdxlxMap.get(e.getCmPlanObjectType()));
            e.setCmPlanFrequency(jhpcMap.get(e.getCmPlanFrequency()));
            if (Objects.nonNull(e.getCmJobStatus())) {
                if (e.getCmJobStatus() == 2) {
                    if (e.getCmJobCondition() == 1) {
                        e.setStatus("进行中(正常)");
                    } else {
                        e.setStatus("进行中(超期)");
                    }
                } else if (e.getCmJobStatus() == 3) {
                    if (e.getCmJobCondition() == 1) {
                        e.setStatus("已巡检(正常)");
                    } else {
                        e.setStatus("已巡检(超时)");
                    }
                } else {
                    e.setStatus("待巡检");
                }
            }
        });
        List<DrainInspectionMnJobExcel> excelList = new ArrayList<>();
        planList.forEach(item -> {
            DrainInspectionMnJobExcel excel = new DrainInspectionMnJobExcel();
            BeanUtils.copyProperties(item, excel);
            //TODO 字典翻译处理
            excelList.add(excel);
        });

        String fileName = "巡检任务记录.xlsx";
        HttpResponseUtil.setResponseHeaders(response, fileName);
        EasyExcel.write(response.getOutputStream(), DrainInspectionMnJobExcel.class)
                .sheet("巡检任务")
                .doWrite(() -> excelList);
    }

    @Override
    public DrainInspectionMnJobVo planType() {
        LambdaQueryWrapper<DrainInspectionMnJob> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DrainInspectionMnJob::getCmUsable, false);
        queryWrapper.eq(DrainInspectionMnJob::getCmJobStatus, 1);
        queryWrapper.and(wrapper -> wrapper.eq(DrainInspectionMnJob::getCmPlanType, "jhlx1")
                .or().eq(DrainInspectionMnJob::getCmPlanType, "jhlx3")
        );
        LambdaQueryWrapper<DrainInspectionRlUser> userQueryWrapper = new LambdaQueryWrapper<>();
        userQueryWrapper.eq(DrainInspectionRlUser::getCmUserId, Long.parseLong(SecurityUtils.getPrincipal().getUserId()));
        List<DrainInspectionRlUser> drainInspectionRlUsers = userMapper.selectList(userQueryWrapper);
        List<Long> planIds = drainInspectionRlUsers.stream().map(DrainInspectionRlUser::getCmPlanId).collect(Collectors.toList());
        if (planIds.isEmpty()) {
            queryWrapper.eq(DrainInspectionMnJob::getCmKid, -1);
        } else {
            queryWrapper.in(DrainInspectionMnJob::getCmPlanId, planIds);
        }

        Long jhl = drainInspectionMnJobMapper.selectCount(queryWrapper);
        LambdaQueryWrapper<DrainInspectionMnJob> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(DrainInspectionMnJob::getCmUsable, false);
        queryWrapper1.eq(DrainInspectionMnJob::getCmJobStatus, 1);
        queryWrapper1.and(wrapper -> wrapper.eq(DrainInspectionMnJob::getCmPlanType, "jhlx2")
                .or().eq(DrainInspectionMnJob::getCmPlanType, "jhlx4")
        );
        LambdaQueryWrapper<DrainInspectionRlUser> userQueryWrapper1 = new LambdaQueryWrapper<>();
        userQueryWrapper1.eq(DrainInspectionRlUser::getCmUserId, Long.parseLong(SecurityUtils.getPrincipal().getUserId()));
        List<DrainInspectionRlUser> drainInspectionRlUsers1 = userMapper.selectList(userQueryWrapper1);
        List<Long> planIds1 = drainInspectionRlUsers1.stream().map(DrainInspectionRlUser::getCmPlanId).collect(Collectors.toList());
        if (planIds1.isEmpty()) {
            queryWrapper1.eq(DrainInspectionMnJob::getCmKid, -1);
        } else {
            queryWrapper1.in(DrainInspectionMnJob::getCmPlanId, planIds1);
        }

        Long jhw = drainInspectionMnJobMapper.selectCount(queryWrapper1);
        DrainInspectionMnJobVo s = new DrainInspectionMnJobVo();
        s.setJhlsl(jhl);
        s.setJhwsl(jhw);
        return s;
    }

    @Override
    public Result<Object> planXcx(Page<DrainInspectionMnJob> page, String stutas, String plan, String keywords, Integer cmPlanCategory) {
        LambdaQueryWrapper<DrainInspectionMnJob> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DrainInspectionMnJob::getCmUsable, false);
        queryWrapper.eq(StringUtils.isNotEmpty(stutas), DrainInspectionMnJob::getCmJobStatus, stutas);
        if (plan.equals("1")) {
            queryWrapper.and(wrapper -> wrapper.eq(StringUtils.isNotEmpty(plan), DrainInspectionMnJob::getCmPlanType, "jhlx1")
                    .or().eq(StringUtils.isNotEmpty(plan), DrainInspectionMnJob::getCmPlanType, "jhlx3"));
        } else {
            queryWrapper.and(wrapper -> wrapper.eq(StringUtils.isNotEmpty(plan), DrainInspectionMnJob::getCmPlanType, "jhlx2")
                    .or().eq(StringUtils.isNotEmpty(plan), DrainInspectionMnJob::getCmPlanType, "jhlx4")
            );
        }
        LambdaQueryWrapper<DrainInspectionRlUser> userQueryWrapper = new LambdaQueryWrapper<>();
        userQueryWrapper.eq(DrainInspectionRlUser::getCmUserId, Long.parseLong(SecurityUtils.getPrincipal().getUserId()));
        List<DrainInspectionRlUser> drainInspectionRlUsers = userMapper.selectList(userQueryWrapper);
        List<Long> planIds = drainInspectionRlUsers.stream().map(DrainInspectionRlUser::getCmPlanId).collect(Collectors.toList());
        if (planIds.isEmpty()) {
            return Result.content(new Page<DrainInspectionMnJob>());
        } else {
            queryWrapper.in(DrainInspectionMnJob::getCmPlanId, planIds);
        }
        if (StringUtils.isNotBlank(keywords)) {
            queryWrapper.and(e ->
                    e.like(DrainInspectionMnJob::getCmJobNo, keywords).or()
                            .like(DrainInspectionMnJob::getCmPlanName, keywords)
            );
        }
        if (Objects.nonNull(cmPlanCategory)) {
            queryWrapper.eq(DrainInspectionMnJob::getCmPlanCategory, cmPlanCategory);
        }
        queryWrapper.orderByDesc(DrainInspectionMnJob::getCmJobStartDate);
        Page<DrainInspectionMnJob> planList = this.page(page, queryWrapper);
        return Result.content(planList);
    }

    @Override
    public boolean start(String id, String jd, String wd) {
        DrainInspectionMnJob drainInspectionMnJob = drainInspectionMnJobMapper.selectById(id);
        if (drainInspectionMnJob.getCmJobStatus() == 2) {
            return true;
        }
        LambdaUpdateWrapper<DrainInspectionMnJob> queryWrapper = new LambdaUpdateWrapper<>();
        queryWrapper.eq(DrainInspectionMnJob::getCmKid, id);
        queryWrapper.set(DrainInspectionMnJob::getCmJobStatus, 2);
        queryWrapper.set(DrainInspectionMnJob::getCmJobIsStop, 0);
        queryWrapper.set(DrainInspectionMnJob::getCmJobActualStartDate, new Date());
        queryWrapper.set(DrainInspectionMnJob::getCmStartLng, jd);
        queryWrapper.set(DrainInspectionMnJob::getCmStartLat, wd);
        boolean update = this.update(queryWrapper);
        this.getStutasByCondition(id);
        return update;
    }

    @Override
    public boolean pause(String id) {
        LambdaUpdateWrapper<DrainInspectionMnJob> queryWrapper = new LambdaUpdateWrapper<>();
        queryWrapper.eq(DrainInspectionMnJob::getCmKid, id);
        queryWrapper.set(DrainInspectionMnJob::getCmJobIsStop, 1);
        return this.update(queryWrapper);
    }

    @Override
    public boolean continues(String id) {
        LambdaUpdateWrapper<DrainInspectionMnJob> queryWrapper = new LambdaUpdateWrapper<>();
        queryWrapper.eq(DrainInspectionMnJob::getCmKid, id);
        queryWrapper.set(DrainInspectionMnJob::getCmJobIsStop, 0);
        return this.update(queryWrapper);
    }

    @Override
    public boolean stop(String id, String jd, String wd) {
        LambdaUpdateWrapper<DrainInspectionMnJob> queryWrapper = new LambdaUpdateWrapper<>();
        queryWrapper.eq(DrainInspectionMnJob::getCmKid, id);
        queryWrapper.set(DrainInspectionMnJob::getCmJobStatus, 3);
        queryWrapper.set(DrainInspectionMnJob::getCmJobActualEndDate, new Date());
        queryWrapper.set(DrainInspectionMnJob::getCmStopLng, jd);
        queryWrapper.set(DrainInspectionMnJob::getCmStopLat, wd);
        boolean update = this.update(queryWrapper);
        this.getStutasByCondition(id);
        return update;
    }


    @Override
    public Long myJobSize() {
        HerodotusUserDetails principal = SecurityUtils.getPrincipal();
        String useId = principal.getUserId();
        LambdaQueryWrapper<DrainInspectionRlUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DrainInspectionRlUser::getCmUserId, useId);
        List<DrainInspectionRlUser> userList = drainInspectionRlUserMapper.selectList(queryWrapper);
        List<Long> planIds = userList.stream().map(DrainInspectionRlUser::getCmPlanId).collect(Collectors.toList());
        LambdaQueryWrapper<DrainInspectionMnJob> queryWrapper1 = new LambdaQueryWrapper<>();
        if (planIds.isEmpty()) {
            return 0L;
        } else {
            queryWrapper1.eq(DrainInspectionMnJob::getCmJobStatus, 1);
            queryWrapper1.in(DrainInspectionMnJob::getCmPlanId, planIds);
            return drainInspectionMnJobMapper.selectCount(queryWrapper1);
        }
    }

    @Override
    public Map<String, Object> myData() {
        LocalDate lastYear = LocalDate.now();
        HerodotusUserDetails principal = SecurityUtils.getPrincipal();
        String useId = principal.getUserId();
        LambdaQueryWrapper<DrainInspectionRlUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DrainInspectionRlUser::getCmUserId, useId);
        List<DrainInspectionRlUser> drainInspectionRlUsers = drainInspectionRlUserMapper.selectList(queryWrapper);
        List<Long> planIds = drainInspectionRlUsers.stream().map(DrainInspectionRlUser::getCmPlanId).collect(Collectors.toList());
        LambdaQueryWrapper<DrainInspectionMnJob> queryWrapper1 = new LambdaQueryWrapper<>();
        if (planIds.isEmpty()) {
            queryWrapper1.eq(DrainInspectionMnJob::getCmKid, -1);
        } else {
            queryWrapper1.in(DrainInspectionMnJob::getCmPlanId, planIds);
        }
        List<DrainInspectionMnJob> drainInspectionMnJobs = drainInspectionMnJobMapper.selectList(queryWrapper1);
        Map<String, Long> collect = drainInspectionMnJobs.stream().collect(Collectors.groupingBy(o -> DateUtil.dateToDateString(o.getCmJobStartDate(), "yyyy-MM"), Collectors.counting()));
        Integer[] retArray = new Integer[6];
        String[] strings = new String[6];
        for (int i = 0; i < 6; i++) {
            LocalDate plusMonths = lastYear.minusMonths(i);
            String s = DateUtil.formatLocalDate(plusMonths, DateUtil.yyyy_MM);
            Integer sum = 0;
            if (collect.containsKey(s)) {
                sum = collect.get(s).intValue();
            }
            retArray[i] = sum;
            strings[i] = s;
        }
        Map<String, Object> s = Maps.newHashMap();
        s.put("月份", strings);
        s.put("数量", retArray);
        return s;
    }


    @Override
    public Result<Object> planPc(String stutas) {
        LambdaQueryWrapper<DrainInspectionMnJob> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DrainInspectionMnJob::getCmUsable, false);
        queryWrapper.eq(StringUtils.isNotEmpty(stutas), DrainInspectionMnJob::getCmJobStatus, stutas);
        return Result.content(drainInspectionMnJobMapper.selectList(queryWrapper));
    }

    @Override
    public DrainInspectionMnJob detail(Long id) {
        DrainInspectionMnJob drainInspectionMnJob = drainInspectionMnJobMapper.selectById(id);
        drainInspectionMnJob.setCmPlanAreaPoints(drainInspectionMnPlanMapper.selectById(drainInspectionMnJob.getCmPlanId()).getCmPlanAreaPoints());
        LambdaQueryWrapper<DrainInspectionRlPoint> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DrainInspectionRlPoint::getCmPlanId, drainInspectionMnJob.getCmPlanId());
        queryWrapper.eq(DrainInspectionRlPoint::getCmUsable, false);
        queryWrapper.eq(DrainInspectionRlPoint::getCmPontType, 0);
        List<DrainInspectionRlPoint> drainInspectionRlPoints = drainInspectionRlPointMapper.selectList(queryWrapper);
        drainInspectionRlPoints.stream().forEach(e -> {
            double[] doubles = CoordinateTransformUtils.wgs84togcj02(e.getCmPointLng(), e.getCmPointLat());
            e.setCmPointLat(doubles[1]);
            e.setCmPointLng(doubles[0]);
        });
        LambdaQueryWrapper<DrainInspectionRlPoint> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(DrainInspectionRlPoint::getCmPlanId, drainInspectionMnJob.getCmPlanId());
        queryWrapper1.eq(DrainInspectionRlPoint::getCmUsable, false);
        queryWrapper1.eq(DrainInspectionRlPoint::getCmPontType, 1);
        List<DrainInspectionRlPoint> drainInspectionRlPoints1 = drainInspectionRlPointMapper.selectList(queryWrapper1);
        drainInspectionRlPoints1.stream().forEach(e -> {
            double[] doubles = CoordinateTransformUtils.wgs84togcj02(e.getCmPointLng(), e.getCmPointLat());
            e.setCmPointLat(doubles[1]);
            e.setCmPointLng(doubles[0]);
        });
        drainInspectionMnJob.setAreaPoints(drainInspectionRlPoints);
        drainInspectionMnJob.setObjectPoints(drainInspectionRlPoints1);
        LambdaQueryWrapper<DrainInspectionRlTrack> queryWrapper2 = new LambdaQueryWrapper<>();
        queryWrapper2.eq(DrainInspectionRlTrack::getCmJobId, id);
        HerodotusUserDetails principal = SecurityUtils.getPrincipal();
        String useId = principal.getUserId();
        queryWrapper2.eq(DrainInspectionRlTrack::getCmUserId, useId);
        queryWrapper2.orderByDesc(DrainInspectionRlTrack::getCmAddtime);
        List<DrainInspectionRlTrack> drainInspectionRlTracks = drainInspectionRlTrackMapper.selectList(queryWrapper2);
        drainInspectionRlTracks.stream().forEach(e -> {
            double[] doubles = CoordinateTransformUtils.wgs84togcj02(Double.valueOf(e.getCmTrackLng()), Double.valueOf(e.getCmTrackLat()));
            e.setCmTrackLat(String.valueOf(doubles[1]));
            e.setCmTrackLng(String.valueOf(doubles[0]));
        });
        drainInspectionMnJob.setDrainInspectionRlTracks(drainInspectionRlTracks);
        return drainInspectionMnJob;
    }

    @Override
    public DrainInspectionMnJob detailPc(Long id) {
        DrainInspectionMnJob drainInspectionMnJob = drainInspectionMnJobMapper.selectById(id);
        LambdaQueryWrapper<DrainInspectionRlTrack> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(DrainInspectionRlTrack::getCmJobId, id);
        queryWrapper1.orderByDesc(DrainInspectionRlTrack::getCmAddtime);
        List<DrainInspectionRlTrack> drainInspectionRlTracks = drainInspectionRlTrackMapper.selectList(queryWrapper1);
        drainInspectionMnJob.setCmPlanAreaPoints(drainInspectionMnPlanMapper.selectById(drainInspectionMnJob.getCmPlanId()).getCmPlanAreaPoints());
        List tep = new ArrayList();
        drainInspectionRlTracks.stream().forEach(e -> {
            List ls = new ArrayList();
            String cmTrackLat = e.getCmTrackLat();
            String cmTrackLng = e.getCmTrackLng();
            ls.add(Double.valueOf(cmTrackLng));
            ls.add(Double.valueOf(cmTrackLat));
            tep.add(ls);
        });
        drainInspectionMnJob.setJwd(tep);
        LambdaQueryWrapper<DrainInspectionMnProblem> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DrainInspectionMnProblem::getCmJobId, id);
        List<DrainInspectionMnProblem> drainInspectionMnProblems = drainInspectionMnProblemMapper.selectList(queryWrapper);
        drainInspectionMnJob.setDrainInspectionMnProblems(drainInspectionMnProblems);
        return drainInspectionMnJob;
    }

    @Override
    public DrainInspectionMnJob detailXj(Long id) {
        DrainInspectionMnJob drainInspectionMnJob = drainInspectionMnJobMapper.selectById(id);
        LambdaQueryWrapper<DrainInspectionRlTrack> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(DrainInspectionRlTrack::getCmJobId, id);
        queryWrapper1.orderByDesc(DrainInspectionRlTrack::getCmAddtime);
        List<DrainInspectionRlTrack> drainInspectionRlTracks = drainInspectionRlTrackMapper.selectList(queryWrapper1);
        drainInspectionMnJob.setCmPlanAreaPoints(drainInspectionMnPlanMapper.selectById(drainInspectionMnJob.getCmPlanId()).getCmPlanAreaPoints());
        List tep = new ArrayList();
        drainInspectionRlTracks.stream().forEach(e -> {
            List ls = new ArrayList();
            String cmTrackLat = e.getCmTrackLat();
            String cmTrackLng = e.getCmTrackLng();
            ls.add(Double.valueOf(cmTrackLng));
            ls.add(Double.valueOf(cmTrackLat));
            tep.add(ls);
        });
        drainInspectionMnJob.setJwd(tep);
        LambdaQueryWrapper<DrainInspectionMnProblem> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DrainInspectionMnProblem::getCmJobId, id);
        List<DrainInspectionMnProblem> drainInspectionMnProblems = drainInspectionMnProblemMapper.selectList(queryWrapper);
        drainInspectionMnJob.setDrainInspectionMnProblems(drainInspectionMnProblems);
        drainInspectionMnJob.setDrainInspectionRlTracks(drainInspectionRlTracks);
        return drainInspectionMnJob;
    }

    @Override
    public Result<Object> planXj(String stutas) {
        String date = DateUtil.formatDate(new Date(), "yyyy-MM") + "%";

        LambdaQueryWrapper<DrainInspectionMnJob> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DrainInspectionMnJob::getCmUsable, false);
        if ("3".equals(stutas)) {
            queryWrapper.eq(StringUtils.isNotEmpty(stutas), DrainInspectionMnJob::getCmJobStatus, stutas);
            queryWrapper.apply("CAST(cm_job_actual_start_date as VARCHAR) like {0}", date);

        } else {
            queryWrapper.eq(StringUtils.isNotEmpty(stutas), DrainInspectionMnJob::getCmJobStatus, stutas);
        }
        List<DrainInspectionMnJob> drainInspectionMnJobs = drainInspectionMnJobMapper.selectList(queryWrapper);
        drainInspectionMnJobs.stream().forEach(e -> {
            DrainInspectionMnPlan drainInspectionMnPlan = drainInspectionMnPlanMapper.selectById(e.getCmPlanId());

            if (Objects.nonNull(drainInspectionMnPlan)) {
                e.setCmPlanAreaPoints(drainInspectionMnPlan.getCmPlanAreaPoints());
            }

        });

        if ("2".equals(stutas)) {

            drainInspectionMnJobs.stream().forEach(e -> {
                LambdaQueryWrapper<DrainInspectionRlTrack> queryWrapper1 = new LambdaQueryWrapper<>();
                queryWrapper1.in(DrainInspectionRlTrack::getCmJobId, e.getCmKid());

                e.setDrainInspectionRlTracks(drainInspectionRlTrackMapper.selectList(queryWrapper1));

                LambdaQueryWrapper<DrainInspectionMnProblem> queryWrapper2 = new LambdaQueryWrapper<>();
                queryWrapper2.in(DrainInspectionMnProblem::getCmJobId, e.getCmKid());
                e.setDrainInspectionMnProblems(drainInspectionMnProblemMapper.selectList(queryWrapper2));
            });


        }


        return Result.content(drainInspectionMnJobs);
    }

    @Override
    public Long[] planSl() {

        Long aLong = planSlXc("1");
        Long aLong2 = planSlXc("2");
        Long aLong3 = planSlXc("3");
        Long[] arr1 = {aLong, aLong2, aLong3};
        return arr1;
    }


    Long planSlXc(String stutas) {
        String date = DateUtil.formatDate(new Date(), "yyyy-MM") + "%";

        LambdaQueryWrapper<DrainInspectionMnJob> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DrainInspectionMnJob::getCmUsable, false);
        if ("3".equals(stutas)) {
            queryWrapper.eq(StringUtils.isNotEmpty(stutas), DrainInspectionMnJob::getCmJobStatus, stutas);
            queryWrapper.apply("CAST(cm_job_actual_start_date as VARCHAR) like {0}", date);

        } else {
            queryWrapper.eq(StringUtils.isNotEmpty(stutas), DrainInspectionMnJob::getCmJobStatus, stutas);
        }


        return drainInspectionMnJobMapper.selectCount(queryWrapper);
    }


    @Override
    public DrainInspectionMnJob planJxz() {
        DrainInspectionMnJob s = new DrainInspectionMnJob();
        LambdaQueryWrapper<DrainInspectionMnJob> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DrainInspectionMnJob::getCmUsable, false);
        queryWrapper.eq(DrainInspectionMnJob::getCmJobStatus, "2");
        List<DrainInspectionMnJob> drainInspectionMnJobs = drainInspectionMnJobMapper.selectList(queryWrapper);
        List<DrainInspectionRlTrack> drainInspectionRlTracks = null;
        List<DrainInspectionMnProblem> drainInspectionMnProblems = null;
        List<Long> jobIds = drainInspectionMnJobs.stream().map(DrainInspectionMnJob::getCmKid).collect(Collectors.toList());
        drainInspectionMnJobs.stream().forEach(e -> {
            jobIds.add(e.getCmKid());
        });


        //轨迹
        LambdaQueryWrapper<DrainInspectionRlTrack> queryWrapper1 = new LambdaQueryWrapper<>();
        if (jobIds.isEmpty()) {
            s.setDrainInspectionRlTracks(drainInspectionRlTracks);
            s.setDrainInspectionMnProblems(drainInspectionMnProblems);
        } else {
            queryWrapper1.in(DrainInspectionRlTrack::getCmJobId, jobIds);
            drainInspectionRlTracks = drainInspectionRlTrackMapper.selectList(queryWrapper1);
            s.setDrainInspectionRlTracks(drainInspectionRlTracks);


            LambdaQueryWrapper<DrainInspectionMnProblem> queryWrapper2 = new LambdaQueryWrapper<>();
            queryWrapper2.in(DrainInspectionMnProblem::getCmJobId, jobIds);
            drainInspectionMnProblems = drainInspectionMnProblemMapper.selectList(queryWrapper2);
            s.setDrainInspectionMnProblems(drainInspectionMnProblems);
        }


        return s;

    }


    /**
     * 统计我的巡视
     *
     * @return MiniProgramCountVo 统计
     */
    @Override
    public MiniProgramCountVo countMyXs() {
        MiniProgramCountVo xsVo = new MiniProgramCountVo();
        xsVo.setBasisName("我的巡视");
        LambdaQueryWrapper<DrainInspectionMnJob> queryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<DrainInspectionRlUser> userQueryWrapper = new LambdaQueryWrapper<>();
        userQueryWrapper.eq(DrainInspectionRlUser::getCmUserId, Long.parseLong(SecurityUtils.getPrincipal().getUserId()));
        List<DrainInspectionRlUser> drainInspectionRlUsers = userMapper.selectList(userQueryWrapper);
        List<Long> planIds = drainInspectionRlUsers.stream().map(DrainInspectionRlUser::getCmPlanId).collect(Collectors.toList());
        if (planIds.isEmpty()) {
            xsVo.setCnt(0L);
        } else {
            queryWrapper.in(DrainInspectionMnJob::getCmPlanId, planIds);
            queryWrapper.eq(DrainInspectionMnJob::getCmJobStatus, 1);
            queryWrapper.eq(DrainInspectionMnJob::getCmUsable, false);
            queryWrapper.eq(DrainInspectionMnJob::getCmPlanCategory, 0);
            xsVo.setCnt(drainInspectionMnJobMapper.selectCount(queryWrapper));
        }

        xsVo.setCmPlanCategory(0);
        return xsVo;
    }

    /**
     * 统计我的检查
     *
     * @return MiniProgramCountVo 统计
     */
    @Override
    public MiniProgramCountVo countMyJc() {
        MiniProgramCountVo jcVo = new MiniProgramCountVo();
        jcVo.setBasisName("我的检查");
        LambdaQueryWrapper<DrainInspectionMnJob> queryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<DrainInspectionRlUser> userQueryWrapper = new LambdaQueryWrapper<>();
        userQueryWrapper.eq(DrainInspectionRlUser::getCmUserId, Long.parseLong(SecurityUtils.getPrincipal().getUserId()));
        List<DrainInspectionRlUser> drainInspectionRlUsers = userMapper.selectList(userQueryWrapper);
        List<Long> planIds = drainInspectionRlUsers.stream().map(DrainInspectionRlUser::getCmPlanId).collect(Collectors.toList());
        if (planIds.isEmpty()) {
            jcVo.setCnt(0L);
        } else {
            queryWrapper.in(DrainInspectionMnJob::getCmPlanId, planIds);
            queryWrapper.eq(DrainInspectionMnJob::getCmJobStatus, 1);
            queryWrapper.eq(DrainInspectionMnJob::getCmUsable, false);
            queryWrapper.eq(DrainInspectionMnJob::getCmPlanCategory, 2);
            jcVo.setCnt(drainInspectionMnJobMapper.selectCount(queryWrapper));
        }

        jcVo.setCmPlanCategory(2);
        return jcVo;
    }


}
